package com.itheima.controller;

import com.itheima.common.constant.MessageConstant;
import com.itheima.common.entity.PageResult;
import com.itheima.common.entity.QueryPageBean;
import com.itheima.common.entity.Result;
import com.itheima.common.utils.AliOssUtil;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.UUID;

/**
 * @author 32338
 * @date 2024-05-03 20:12:09
 * @description
 */
@RestController
@RequestMapping("/setmeal")
@Slf4j
public class SetmealController {
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private AliOssUtil aliOssUtil;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * @description
     ***:新增套餐
     **/
    @PostMapping("/add")
    public Result add(@RequestBody Setmeal setmeal, @RequestParam List<Integer> checkgroupIds){
        log.info("新增套餐：{}",setmeal.toString());

        setmealService.add(setmeal,checkgroupIds);
        String img = setmeal.getImg();
        //获取传入阿里云的名字
        String objectName = img.substring(img.lastIndexOf("/"));
        //将获取到的阿里云图片的名字存入到Redis中去
        redisTemplate.opsForSet().add("userImage",objectName);
        return new Result(true,MessageConstant.ADD_SETMEAL_SUCCESS);
    }

    /**
     * @description
     ***:删除套餐
     **/
    @GetMapping("/deleteById")
    public Result delete(Integer id){
        log.info("根据id删除套餐：{}",id);

        setmealService.deleteById(id);

        return new Result(true,MessageConstant.DELETE_SETMEAL_SUCCESS);
    }

    /**
     * @description
     ***:修改套餐
     **/
    @PostMapping("/edit")
    public Result edit(@RequestBody Setmeal setmeal, @RequestParam List<Integer> checkGroupIds){
        log.info("修改套餐：{}",setmeal);

        setmealService.edit(setmeal,checkGroupIds);

        return new Result(true,MessageConstant.EDIT_SETMEAL_SUCCESS);
    }

    /**
     * @description
     ***:根据id查询套餐
     **/
    @GetMapping("/findById")
    public Result findById(Integer id){
        log.info("根据id查询套餐：{}",id);
        Setmeal setmeal=setmealService.findById(id);
        return new Result(true,MessageConstant.QUERY_SETMEAL_SUCCESS,setmeal);
    }

    /**
     * @description
     ***:分页查询
     **/
    @PostMapping ("/findPage")
    public PageResult page(@RequestBody QueryPageBean queryPageBean){
        log.info("分页查询套餐");
        PageResult pageResult=setmealService.page(queryPageBean);
        return pageResult;
    }

    /**
     * @description
     ***:type代表类型 value查询条件
     **/
    @GetMapping("/findItemOrGroup")
    public Result findItemOrGroup(String type,String value){
        log.info("type代表类型 value查询条件: {} ,{}",type,value);
        List<?> list = setmealService.findItemOrGroup(type,value);
        return new Result(true,MessageConstant.QUERY_SETMEAL_SUCCESS,list);
    }

    /**
     * @description
     ***:文件上传
     **/
    @PostMapping("/upload")
    public Result upload(MultipartFile imgFile){
        log.info("文件上传");
        try {
            String originalFileName=imgFile.getOriginalFilename();
            //拿到后缀名
            String extension = originalFileName.substring(originalFileName.lastIndexOf("."));
            String objectName= UUID.randomUUID().toString()+extension;
            redisTemplate.opsForSet().add("image",objectName);//图片上传阿里云的同时也把图片文件名上传到Redis中去
            String filePath=aliOssUtil.upload(imgFile.getBytes(), objectName);

            return new Result(true, MessageConstant.PIC_UPLOAD_SUCCESS,filePath);
        } catch (IOException e) {
            log.error(MessageConstant.PIC_UPLOAD_FAIL,e);
        }

        return new Result(false, MessageConstant.PIC_UPLOAD_FAIL);
    }
}
